{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 批量创建文件夹"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import openpyxl\n",
    "\n",
    "# 指定 Excel 文件路径\n",
    "excel_file_path = ''\n",
    "root_dir = ''\n",
    "\n",
    "# 加载 Excel 工作簿\n",
    "workbook = openpyxl.load_workbook(excel_file_path)\n",
    "\n",
    "\n",
    "# 选择第一个工作表\n",
    "sheet = workbook.active\n",
    "\n",
    "# 遍历第一列的每个单元格\n",
    "for row in sheet.iter_rows(min_col=1, max_col=1, values_only=True):\n",
    "    cell_value = row[0]\n",
    "    \n",
    "    # 检查单元格值是否为空\n",
    "    if cell_value is not None:\n",
    "        # 创建文件夹路径\n",
    "        folder_path = os.path.join(root_dir, str(cell_value))\n",
    "        \n",
    "        # 创建文件夹（如果不存在）\n",
    "        os.makedirs(folder_path, exist_ok=True)\n",
    "        print(f\"Created folder: {folder_path}\")\n",
    "    else:\n",
    "        print(\"Skipping empty cell\")\n",
    "\n",
    "print(\"Folder creation completed.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除空文件夹"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "def delete_empty_folders(path):\n",
    "    # 检查路径是否存在\n",
    "    if not os.path.exists(path):\n",
    "        print(f\"路径 {path} 不存在。\")\n",
    "        return\n",
    "\n",
    "    # 检查路径是否是文件夹\n",
    "    if not os.path.isdir(path):\n",
    "        print(f\"{path} 不是一个文件夹。\")\n",
    "        return\n",
    "\n",
    "    # 获取路径中的所有文件和文件夹\n",
    "    with os.scandir(path) as entries:\n",
    "        for entry in entries:\n",
    "            # 构建完整的文件/文件夹路径\n",
    "            full_path = entry.path\n",
    "\n",
    "            # 如果是文件夹，递归地检查和删除空文件夹\n",
    "            if entry.is_dir():\n",
    "                delete_empty_folders(full_path)\n",
    "\n",
    "                # 如果文件夹现在是空的，删除它\n",
    "                if not os.listdir(full_path):\n",
    "                    # print(f\"删除空文件夹：{full_path}\")\n",
    "                    os.rmdir(full_path)\n",
    "\n",
    "# 设置目录\n",
    "current_dir =''\n",
    "delete_empty_folders(current_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件夹复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "def copy_folder_names_only(src_dir, dest_dir):\n",
    "    # 确保目标目录存在，如果不存在则创建\n",
    "    if not os.path.exists(dest_dir):\n",
    "        os.makedirs(dest_dir)\n",
    "\n",
    "    # 遍历源目录中的所有文件和子目录\n",
    "    for root, dirs, files in os.walk(src_dir):\n",
    "        # 跳过文件，只处理目录\n",
    "        for dir_name in dirs:\n",
    "            # 构建源目录中的完整路径\n",
    "            src_path = os.path.join(root, dir_name)\n",
    "\n",
    "            # 构建目标目录中的相对路径（与源目录结构相同）\n",
    "            rel_path = os.path.relpath(src_path, src_dir)\n",
    "            dest_path = os.path.join(dest_dir, rel_path)\n",
    "\n",
    "            # 确保目标路径的父目录存在\n",
    "            os.makedirs(os.path.dirname(dest_path), exist_ok=True)\n",
    "\n",
    "            # 在这里，你可以选择创建一个空文件或文件夹来表示源目录。\n",
    "            # 但由于我们只关心名称，所以这里只是打印出来。\n",
    "            # print(f\"Found directory: {src_path}, would be copied to: {dest_path}\")\n",
    "            # 如果需要，你可以取消注释以下行来创建空文件夹\n",
    "            os.makedirs(dest_path)\n",
    "\n",
    "\n",
    "# 使用函数\n",
    "src_dir = \"\"  # 替换为你的源目录路径\n",
    "dest_dir = \"\"  # 替换为你的目标目录路径\n",
    "copy_folder_names_only(src_dir, dest_dir)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
